package com.user.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.user.utils.*;
import java.util.Iterator;
import org.springframework.web.bind.annotation.RequestMapping;
import com.user.bean.*;
import com.user.service.MayuanService;

/*控制层：MayuanController*/
@Controller
@RequestMapping("/mayuan")
public class MayuanController{

//	注入MayuanService对象
	@Autowired
	private MayuanService mayuanService;
	@Autowired
	private HttpServletRequest request;
//	查询数据返回到/page/mayuan/mayuanItem.jsp页面
@RequestMapping("/findAll")
public String findAll(Model model,HttpServletRequest request,Mayuan mayuan,@RequestParam(required=false,defaultValue="true") Boolean isDistance) {
List<Mayuan> list = mayuanService.findAll(mayuan,isDistance);
//	将list集合放进request域中带给/page/mayuan/mayuanItem.jsp页面
		request.setAttribute("list", list);
//	将条件对象放进request域中带给/page/mayuan/mayuanItem.jsp页面
		request.setAttribute("query", mayuan);
return "/page/mayuan/mayuanItem.jsp";
}
//	根据mayuanItem.jsp页面传递的id，从数据库中查询出mayuan对象保存到model域，转发到mayuanUpdate.jsp页面
@RequestMapping("/saveById")
public String saveById(Model model,Integer id) {
Mayuan mayuan = mayuanService.findById(id);
model.addAttribute("mayuan", mayuan);
return "/page/mayuan/mayuanUpdate.jsp";
}
//	删除数据
	public void deleteById(String id) {
		if (id!=null) {
			//	删除数据库中mayuan中的对应id的数据
			mayuanService.deleteById(Integer.valueOf(id));
		}
	}
//	根据mayuanItem.jsp页面传递的id，调用mayuanServlet#deleteById(id)方法删除mayuan表中的数据，重定向到findAll请求
@RequestMapping("/deleteById")
public String deleteById(HttpServletRequest request) {
//		单条删除
		deleteById(request.getParameter("id"));
//		批量删除
		String ids = request.getParameter("ids");
		if (ids!=null) {
			for (String id : ids.split(",")) {
				deleteById(id);
			}
		}
return "redirect:findAll";
}

//	根据mayuanUpdate.jsp页面传递的数据，更新数据库，重定向到findAll请求
	@RequestMapping("/updateById")
	public String updateById(Mayuan mayuan) {
		try {
			mayuanService.updateByIdSelective(mayuan);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("更新失败");
		}
		return "redirect:findAll";
	}

//	根据mayuanInsert.jsp页面传递的数据，封装在mayuan对象中插入数据库，重定向到重定向到findAll请求
	@RequestMapping("/insert")
	public String insert(Mayuan mayuan) {
		try {
			mayuanService.insert(mayuan);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("添加失败");
		}
		return "redirect:findAll";
	}
}